package client;

import java.awt.BorderLayout;

import java.awt.Frame;
import java.awt.FlowLayout;

import java.net.MalformedURLException;
import java.rmi.*;
import java.rmi.server.*;
import server.*;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JOptionPane;
//import javax.swing.JOptionJane;
import javax.swing.border.EmptyBorder;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class NetBar extends JDialog {

	private final JPanel contentPanel = new JPanel();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			NetBar dialog = new NetBar(null,"NET BAR",true);
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 * @throws NotBoundException 
	 * @throws RemoteException 
	 * @throws MalformedURLException 
	 */
	public NetBar(Frame hostFrame,String title,boolean dModal) throws MalformedURLException, RemoteException, NotBoundException {
		super(hostFrame,title,dModal);
		setBounds(100, 100, 450, 300);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);
		
		final Person p=new Person();
		
		Configuration config=new Configuration();
		String rmiAddress=config.getRmiAddress();
		final EntertainInterface ei = (EntertainInterface) Naming.lookup(rmiAddress+"EntertainmentSettling");
		

		{
			JButton button = new JButton("玩游戏");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						if(!ei.hpJudge(p.getPID()))
						{
							JOptionPane.showMessageDialog(null,"HP不足！"," ",JOptionPane.OK_CANCEL_OPTION);		
						}
						else if(!ei.moneyJudge(p.getPID()))
						{
							JOptionPane.showMessageDialog(null,"money不足！"," ",JOptionPane.OK_CANCEL_OPTION);
						}
						else
						{
							ei.settling(p.getPID(), 10,40,20,'-','+','-');
							JOptionPane.showMessageDialog(null,"Hp"+"-10\n"+"Hap"+"+60\n"+"money"+"-20"," ",JOptionPane.OK_CANCEL_OPTION);
						}
					} catch (RemoteException e1) {
						e1.printStackTrace();
					}

				}
			});
			button.setBounds(83, 75, 93, 23);
			contentPanel.add(button);
		}
		{
			JButton btnNewButton = new JButton("\u770B\u7535\u5F71");
			btnNewButton.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						if(!ei.hpJudge(p.getPID()))
						{
							JOptionPane.showMessageDialog(null,"HP不足！"," ",JOptionPane.OK_CANCEL_OPTION);		
						}
						else if(!ei.moneyJudge(p.getPID()))
						{
							JOptionPane.showMessageDialog(null,"money不足！"," ",JOptionPane.OK_CANCEL_OPTION);
						}
						else
						{
							ei.settling(p.getPID(), 10,40,10,'-','+','-');
							JOptionPane.showMessageDialog(null,"Hp"+"-10\n"+"Hap"+"+40\n"+"money"+"-10"," ",JOptionPane.OK_CANCEL_OPTION);
						}
					} catch (RemoteException e2) {
						// TODO Auto-generated catch block
						e2.printStackTrace();
					}
					
				}
			});
			btnNewButton.setBounds(83, 120, 93, 23);
			contentPanel.add(btnNewButton);
		}
		{
			JButton button = new JButton("浏览网页");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						if(!ei.hpJudge(p.getPID()))
						{
							JOptionPane.showMessageDialog(null,"HP不足！"," ",JOptionPane.OK_CANCEL_OPTION);		
						}
						else if(!ei.moneyJudge(p.getPID()))
						{
							JOptionPane.showMessageDialog(null,"money不足！"," ",JOptionPane.OK_CANCEL_OPTION);
						}
						else
						{
							ei.settling(p.getPID(), 0,10,10,'-','+','-');
							JOptionPane.showMessageDialog(null,"Hp"+"  0  \n"+"Hap"+"+10\n"+"money"+"-10"," ",JOptionPane.OK_CANCEL_OPTION);
						}
					} catch (RemoteException e3) {
						e3.printStackTrace();
					}
				}
			});
			button.setBounds(258, 120, 93, 23);
			contentPanel.add(button);
		}
		{
			JButton button = new JButton("聊天");
			button.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						if(!ei.hpJudge(p.getPID()))
						{
							JOptionPane.showMessageDialog(null,"HP不足！"," ",JOptionPane.OK_CANCEL_OPTION);		
						}
						else if(!ei.moneyJudge(p.getPID()))
						{
							JOptionPane.showMessageDialog(null,"money不足！"," ",JOptionPane.OK_CANCEL_OPTION);
						}
						else
						{
							ei.settling(p.getPID(), 10,60,200,'-','+','-');
							JOptionPane.showMessageDialog(null,"Hp"+"-10\n"+"Hap"+"+60\n"+"money"+"-200"," ",JOptionPane.OK_CANCEL_OPTION);
						}
					} catch (RemoteException e4) {
						// TODO Auto-generated catch block
						e4.printStackTrace();
					}
				}
			});
			button.setBounds(258, 75, 93, 23);
			contentPanel.add(button);
		}
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				JButton okButton = new JButton("OK");
				okButton.setActionCommand("OK");
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
				okButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
				cancelButton.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent arg0) {
						dispose();
					}
				});
			}
		}

		JLabel lblNewLabel = new JLabel(new ImageIcon("src/client/images/网吧.jpg"));
		lblNewLabel.setBounds(0, 0, 434, 268);
		contentPanel.add(lblNewLabel);
	}

}
